
	package com.bitBusy.manageyourriches.utilityClasses.adapters;

	import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bitBusy.manageyourriches.R;
import com.bitBusy.manageyourriches.activity.AlarmActivity;
import com.bitBusy.manageyourriches.alarmSystem.AlarmManagement;
import com.bitBusy.manageyourriches.database.contactClasses.PaymentReminder;
import com.bitBusy.manageyourriches.database.managers.DateTimeManager;
import com.bitBusy.manageyourriches.utilityClasses.dialogboxes.DialogBoxPrompts;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayButtonValueUsers;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayMessages;

	/**
	 * @author SumaHarsha
	 * 
	 */
	public class EditReminderListAdapter extends BaseAdapter implements DisplayButtonValueUsers{

		
			
		private static final String EDIT = "Edit";
		
		private static final String DELETE = "Delete";
		
		private static final String DELETEDREMINDER = "Reminder has been deleted";
		
			
	
		
		/** keyset array*/
		private ArrayList<?> myReminders;
		
		private Context myContext;
		
		private int myPosition;
		
		public EditReminderListAdapter(Context the_context, ArrayList<?> the_reminders) {
			myContext = the_context;
			myReminders = the_reminders;
		}


		public int getCount() {
			return (myReminders==null?0:myReminders.size());
		}

		public Object getItem(int position) {
		return (myReminders != null)?myReminders.get(position):null;
		}

		public long getItemId(int position) {
				return position;
		}

		public View getView(final int position, View the_convertView, ViewGroup parent) {
			
			  if (the_convertView == null) {
			   LayoutInflater inf = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			   the_convertView = inf.inflate(R.layout.editreminder_rowformat, null);
			   }
		
			Object item = getItem(position);
				TextView date = (TextView) the_convertView.findViewById(R.id.editRemindersActivity_date);
				TextView time = (TextView) the_convertView.findViewById(R.id.editRemindersActivity_time);
				TextView note = (TextView) the_convertView.findViewById(R.id.editRemindersActivity_note);
				ImageButton editButton = (ImageButton) the_convertView.findViewById(R.id.editRemindersActivity_editDeleteImg);

				
				if (item instanceof PaymentReminder && item != null)
				{		
					PaymentReminder reminder = (PaymentReminder)item;
					date.setText(DateTimeManager.getDate(reminder.getDay(), reminder.getMonth()+1, reminder.getYear()));
					time.setText(DateTimeManager.getTime(reminder.getHour(), (reminder.getMinute())));
					note.setText(reminder.getNote());
					setEditListener(editButton, position, this);
					if (position % 2 == 1)
					{
						date.setBackgroundColor(Color.LTGRAY);
						time.setBackgroundColor(Color.LTGRAY);
						note.setBackgroundColor(Color.LTGRAY);
					}
				}
				else if (item instanceof Title && item != null)
				{
					Title title = (Title)item;
					date.setText(title.getDateHeader());
					date.setTextColor(Color.RED);
					date.setBackgroundColor(Color.TRANSPARENT);
					time.setText(title.getTimeHeader());
					time.setTextColor(Color.RED);
					time.setBackgroundColor(Color.TRANSPARENT);
					note.setText(title.getNoteHeader());
					note.setTextColor(Color.RED);
					note.setBackgroundColor(Color.TRANSPARENT);
					editButton.setVisibility(View.INVISIBLE);
				}
				return the_convertView;
		}


		

				/* (non-Javadoc)
		 * @see com.bitBusy.manageyourriches.utilityClasses.display.DisplayButtonValueUsers#getButtonClickOnDialog(android.widget.Button)
		 */
		@Override
		public void getButtonClickOnDialog(Button the_button) {
			if (the_button != null)
			{
				String buttonText = the_button.getText().toString();
				Object item = myReminders.get(myPosition);
				PaymentReminder reminder =  item instanceof PaymentReminder?(PaymentReminder) item:null;
				if (buttonText.equals(EDIT))
					{
						Activity currActivity = (Activity)myContext;
						Intent intent = new Intent(currActivity, AlarmActivity.class);
						intent.putExtra(AlarmActivity.ALARMDETAILS, reminder);
						myContext.startActivity(intent);
						//deleteReminder(reminder);

					}
					
					if (buttonText.equals(DELETE))
					{
						deleteReminder(reminder);
						Toast.makeText(myContext.getApplicationContext(), DELETEDREMINDER, Toast.LENGTH_LONG).show();
						
					}
					notifyDataSetChanged();				 	

				}
			}
			
			
		/**
		 * @param reminder
		 */
		private void deleteReminder(PaymentReminder the_reminder) {
		if (AlarmManagement.deleteAlarm(the_reminder))
		{
			myReminders.remove(myPosition);
		}
		//	
		}


		


		/**
		 * Method to set listener for edit button
		 * @param the button
		 */
		private void setEditListener(ImageButton the_button, final int the_position, final DisplayButtonValueUsers the_caller)
		{
			if (the_button != null)
			{
				
				the_button.setOnClickListener(new OnClickListener() { 
					@Override
					public void onClick(View the_view) {
							myPosition = the_position;
						  DisplayMessages.displayDialogWithThreeButtonsOnly(myContext, the_caller,
								  DialogBoxPrompts.DIALOGEDITREMINDERTITLE.getPrompt(), EDIT, DELETE, DialogBoxPrompts.DIALOGCANCELPROMPT.getPrompt());
					}

				});
			}
		} 
		
		/**Class for title*/
		 public static class Title
		{
			
			/**Date header*/
			private static String myDateHeader;
			
			/**Payment Info Header*/
			private static String myTimeHeader;
			
			/**Note header*/
			private static String myNoteHeader;
			
			
			/**
			 * Constructor
			 * @param the_dateTimeHeader
			 * @param the_paymentInfoHeader
			 * @param the_amountHeader
			 * @param the_noteHeader
			 */
			 public Title(String the_dateTimeHeader, String the_timeHeader, String the_noteHeader)
			 {
				 myDateHeader = the_dateTimeHeader;
				 myTimeHeader = the_timeHeader;
				 myNoteHeader = the_noteHeader;
			 }
			 
			 /**Getters*/
			 
			 public String getDateHeader()
			 {
				 return myDateHeader;
			 }
			 
			 public String getTimeHeader()
			 {
				 return myTimeHeader;
			 }
			 public String getNoteHeader()
			 {
				 return myNoteHeader;
			 }
		
			 
		}
		
}
